﻿using UnityEngine;
using System.Collections;

/// <summary>
/// Simple Plane.
/// </summary>
[AddComponentMenu("wsLibrary/Mesh/Simple Plane")]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class wsPlane : MonoBehaviour
{
	void Start()
	{
		MeshFilter filter = gameObject.GetComponent<MeshFilter>();

		if (null == filter.sharedMesh)
			BuildMesh();
	}

	void BuildMesh()
	{
		MeshFilter filter = gameObject.GetComponent<MeshFilter>();
		//MeshRenderer renderer = gameObject.GetComponent<MeshRenderer>();

		Mesh newMesh = new Mesh();
		newMesh.vertices = new Vector3[] { (Vector3.left + Vector3.down) / 2f, (Vector3.left + Vector3.up) / 2f, (Vector3.right + Vector3.down) / 2f, (Vector3.right + Vector3.up) / 2f };
		newMesh.uv = new Vector2[] { Vector2.zero, Vector2.up, Vector2.right, Vector2.up + Vector2.right };
		newMesh.triangles = new int[] { 0, 1, 3, 0, 3, 2 };

		//newMesh.RecalculateBounds();
		//newMesh.RecalculateNormals();

		filter.mesh = newMesh;
		Mesh mesh = filter.sharedMesh;

		Bounds bounds = new Bounds();
		bounds.center = Vector3.zero;
		bounds.size = Vector3.one;

		mesh.bounds = bounds;
	}
}